<template>
	<div class="diy_edit cart_edits" id="cart_edit">
	  <el-form
		:model="form"
		ref="ruleFormRef"
		:rules="rules"
		label-width="auto"
		class="edit_cart_form"
		size="large"
	  >
	    <el-row class="row_e">
				<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="购买数量">
					<el-input-number @change="set_price_count" v-model="form.num"></el-input-number>
				</el-form-item>
			</el-col>
			
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="修改总价">
					<el-input-number v-model="form.price_count" :disabled="true"></el-input-number>
				</el-form-item>
			</el-col>
		</el-row>
		<div class="footer">
		  <el-button type="primary" @click="Onsubmit" size="large"
			>提交</el-button
		  >
		  <el-button @click="cancel()" size="large">取消</el-button>
		</div>
	  </el-form>
	</div>
  </template>
  
  <script setup>
  import { ElMessage } from "element-plus";
  import { ref, onMounted } from "vue";
  import { $post, $get } from "@/api/Api.js";
  import storageHelper from "@/libs/StorageHelper";
  import { useStore } from "vuex";
  import { useRouter } from "vue-router";
  import { $getUrlParams,} from "@/utils/utils.js";
  let router = useRouter();
  let store = useStore();
  let token = storageHelper.getToken() || null;
  let UserInfo = storageHelper.getUserInfo();
  let cart_id = $getUrlParams().cart_id;
  let url_add = ref("/api/cart/add");
  let url_set = ref("/api/cart/set");
  let url_get_obj = ref("/api/cart/get_obj");
  let title = ref("cart");
  let field = ref("cart_id");
  let form = ref({
	num: "",
	price_count: "",
  });
  
  let ruleFormRef = ref(null);
  let rules = ref({
	num: [
	  {
		required: true,
		message: "购买数量不能为空",
		trigger: "blur",
	  },
	],
  });
  
  // 点击取消
  let cancel = () => {
	router.go(-1);
  };
  // 点击提交
  let Onsubmit = async () => {
	let valid = await ruleFormRef.value.validate();
	if (valid) {
	  if (cart_id) {
		// 编辑
		delete form.value.update_time;
		$post(url_set.value + "?cart_id=" + cart_id, form.value).then(
		  (res) => {
			if (res.result) {
			  ruleFormRef.value.resetFields();
			  router.push("/home/cart/table");
			  ElMessage.success("提交成功！");
			} else if (res.error) {
			  ElMessage.error(res.error.message);
			}
		  }
		);
	  } else {
		// 新增
		$post(url_add.value, form.value).then((res) => {
		  if (res.result) {
			ruleFormRef.value.resetFields();
			router.push("/home/cart/table");
			ElMessage.success("提交成功！");
		  } else if (res.error) {
			ElMessage.error(res.error.message);
		  }
		});
	  }
	} else {
	  ElMessage.error(msg);
	}
  };
  let  set_price_count=()=>{
		var obj = form.value;
		form.value.price_count = obj.price * obj.num;
}
  // 获取详情
  let get_cart_obj = async () => {
	var list = await $get(url_get_obj.value + "?cart_id=" + cart_id);
	form.value = list.result.obj;
  };
  onMounted(() => {
	get_cart_obj();
  });
  </script>
  
  <style scoped lang="less">
  .diy_edit {
	width: 100%;
	.edit_cart_form {
	  max-width: 60%;
	  margin: 90px auto;
	  .fn {
		width: 100%;
	  }
	}
	img {
	  width: 100px;
	  height: 100px;
	}
	.edit_list_cart_sell {
	  width: 80%;
	  margin: 90px auto;
	}
	.footer {
	  text-align: center;
	}
  }
  </style>
  